//
//  ReadBookDirectoryVC.m
//  WNZSSQ
//
//  Created by JDKJ on 2018/12/7.
//  Copyright © 2018年 JDKJ. All rights reserved.
//

#import "ReadBookDirectoryVC.h"

@interface ReadBookDirectoryVC () <UITableViewDelegate,UITableViewDataSource>

/**
 //数据源
 */
@property(nonatomic,strong)NSMutableArray *dataArr;


/**
 tableView
 */
@property(nonatomic,weak)UITableView *tableView;

@property(nonatomic,weak)UIButton *orderBtn;

@end

@implementation ReadBookDirectoryVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"目录";
    
    //右侧
    UIButton *orderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    orderBtn.frame = CGRectMake(0, 0, 50, 40);
    [orderBtn setTitle:@"倒序" forState:UIControlStateNormal];
    [orderBtn setTitle:@"正序" forState:UIControlStateSelected];
    orderBtn.titleLabel.font = kFont16;
    [orderBtn setImage:@"正序倒序"];
    [orderBtn setTitleColor:CSWhiteColor forState:UIControlStateNormal];
    [orderBtn addTarget:self action:@selector(order:) forControlEvents:UIControlEventTouchUpInside];
    [orderBtn imageOnTheTitleLeftWithSpace:5];
    self.orderBtn = orderBtn;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:orderBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    [self tableView];
    
    NSIndexPath * dayOne = [NSIndexPath indexPathForRow:_readModel.record.chapter inSection:0];
    [self.tableView scrollToRowAtIndexPath:dayOne atScrollPosition:UITableViewScrollPositionTop animated:YES];

}

-(void)order:(UIButton *)sender
{
    sender.selected = !sender.selected;
    
    [self.tableView reloadData];
}


#pragma mark - 懒加载

-(NSMutableArray *)dataArr
{
    if (!_dataArr) {
        
        _dataArr = [NSMutableArray array];
        
    }
    
    return _dataArr;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        
        UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        
        tableView.delegate=self;
        
        tableView.dataSource=self;
        
        tableView.backgroundColor = [UIColor whiteColor];
        
        tableView.rowHeight = 50;
        tableView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:tableView];
        
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.mas_equalTo(self.view);
            
            
        }];
        
        //        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        
        //注册
        [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        
        
        
        _tableView = tableView;
    }
    return _tableView;
}

#pragma mark - UITableViewDataSource

//行数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

//列数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  _readModel.chapters.count;;
}

//cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = 0;
    cell.textLabel.font = MidFont;
    cell.textLabel.textColor = TextColor;
    
    if (self.orderBtn.selected == YES) {
        cell.textLabel.text = _readModel.chapters[_readModel.chapters.count - indexPath.row - 1].title;
        if (_readModel.chapters.count - indexPath.row - 1 == _readModel.record.chapter) {
            cell.textLabel.textColor = CSColorFromRGB(0xF52C50);
        }
        
    }else
    {
        cell.textLabel.text = _readModel.chapters[indexPath.row].title;
        
        if (indexPath.row == _readModel.record.chapter) {
            cell.textLabel.textColor = CSColorFromRGB(0xF52C50);
        }
    }
    
    return cell;
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [[UIView alloc]initWithFrame:CGRectZero];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 30)];
    headView.backgroundColor = CSColorFromRGB(0xF5F5F5);
    UILabel *allLable = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, SCREENWIDTH - 50, 30)];
    allLable.text = @"正文";
    allLable.textColor = TextColor1;
    allLable.font = [UIFont systemFontOfSize:14];
    [headView addSubview:allLable];
    return headView;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //当离开某行时，让某行的选中状态消失
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([self.catalogDelegate respondsToSelector:@selector(catalog:didSelectChapter:page:)]) {
        [self.catalogDelegate catalog:self didSelectChapter:indexPath.row page:0];
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
